﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using WinnerCMS.BLL;
using WinnerCMS.BLL.GatherEngine;
using WinnerCMS.Common;

#endregion

namespace WinnerCMS.WebUI.Admin.Controllers
{
    public class GatherController : ApiController
    {
        public CategoryGatherResult GatherCategoryPage(CategoryParameter cp)
        {
            if (cp.IDs == null)
            {
                cp.IDs = string.Empty;
            }
            if (cp.NextCategoryUrl == null)
            {
                cp.NextCategoryUrl = string.Empty;
            }
            return new AppMain().GatherCategoryPage(cp);
        }

        public ContentPageGatherResult[] GatherWeb(WebParameter wp)
        {
            var gc = new GatherContentPage(wp);

            var url = wp.Url;


            var urls = url.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
            for (var i = 0; i < urls.Length; i++)
            {
                urls[i] = urls[i];
            }

            var tasks = new Task<ContentPageGatherResult>[urls.Length];
            for (var i = 0; i < urls.Length; i++)
            {
                tasks[i] = Task<ContentPageGatherResult>.Factory.StartNew(gc.GatherPage, urls[i]);
            }

            System.Threading.Tasks.Task.WaitAll(tasks);

            var cpgrList = (from x in tasks select x.Result).ToArray();

            return cpgrList;
        }
    }
}